<script setup>
import { ref,reactive,onBeforeMount,computed } from 'vue'
import useSeckillsStore from '../../store/seckillsStore'
import Dialog from './dialog.vue'

const seckillsStore = useSeckillsStore()

// Dialog组件上的ref
const dialog = ref(null)

onBeforeMount(() => {
  // 获取秒杀数据
  seckillsStore.getSeckillsAction()
})

const formatTime = (value) => {
  var date = new Date(Number(value))
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}

// 新增
function addSeckills() {
  // 打开对话框
  seckillsStore.openDialog()
}
// 编辑
function editSeckills(seckills) {
  // console.log(seckills,'编辑')
  dialog.value.editId = seckills.id
  dialog.value.skForm.title = seckills.title
  dialog.value.skForm.begintime = seckills.begintime
  dialog.value.skForm.endtime = seckills.endtime
  dialog.value.skForm.first_cateid = seckills.first_cateid
  dialog.value.skForm.second_cateid = seckills.second_cateid
  dialog.value.skForm.goodsid = seckills.goodsid
  dialog.value.skForm.status = seckills.status
  dialog.value.cateArr.list = [seckills.first_cateid, seckills.second_cateid]
  dialog.value.timeArr.list = [new Date(Number(seckills.begintime)), new Date(Number(seckills.endtime))]
  // 打开对话框
  seckillsStore.openDialog()
}
// 删除
function confirmEvent(id) {
  // console.log(id,'删除')
  seckillsStore.delSeckillsAction(id)
}
</script>

<template>
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item>秒杀管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="right">
      <el-button @click="addSeckills" type="primary">新增秒杀</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="seckillsStore.seckillsList" style="width: 100%" max-height="480">
      <el-table-column prop="id" label="序号" width="80" fixed> </el-table-column>
      <el-table-column
        prop="title"
        label="活动名称"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="goodsid"
        label="商品编号"
        width="120"
      ></el-table-column>
      <el-table-column prop="begintime" label="开始时间" width="200">
        <template v-slot="props">
          <span>{{ formatTime(props.row.begintime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="endtime" label="结束时间" width="200">
        <template v-slot="props">
          <span>{{ formatTime(props.row.endtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="150">
        <template v-slot="props">
          <el-button type="primary" size="small" round @click="editSeckills(props.row)">编辑</el-button>
          <!-- 气泡确认框 -->
          <el-popconfirm
            width="160px"
            title="你确定要删除吗？"
            @confirm="confirmEvent(props.row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 对话框 -->
  <Dialog ref="dialog" />
</template>

<style lang="scss" scoped>

</style>
